﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace FoodFight3D
{
    public class Chef
    {
        public Model ChefModel;
        public int Strength;
        //public food[] FoodsHeld;
        public Vector3 location;
        public float rotationAngle;
        public Vector3 velocity;
        bool moving = false;
        bool poppingUp = false;
        bool poppingDown = false;
        public int holeNum;
        int nextHole;
        Random random2 = new Random();

        public Chef(Model model, float x, float y, float z, int hole)
        {
            ChefModel = model;
            Strength = 14;
            location = new Vector3(x, y, z);
            velocity = new Vector3(0.0f, 0.0f, 0.0f);
            rotationAngle = 0.0f;
            holeNum = hole;
            //foodsheld
        }

        public Chef()
        {
        }

        public void update(Foods[] foodArray, Holes[] pitArray, Kathy player, SoundBank sound, int popUp, int doMove, int foodRandom)
        {
            if(!moving && !poppingDown && !poppingUp)
            {
                if (doMove == 1)
                {
                    moving = true;
                    nextHole = random2.Next(0, 10);
                    for (; pitArray[nextHole].occupied; )
                    {
                        nextHole = (nextHole + 1) % 10;
                    }
                    pitArray[nextHole].occupied = true;
                    pitArray[holeNum].occupied = false;
                    holeNum = nextHole;
                }
                
                if (location.X > player.location.X && location.Z > player.location.Z)
                {
                    rotationAngle = (float)Math.Atan((double)((location.X - player.location.X) / (location.Z - player.location.Z)))
                                + MathHelper.ToRadians(180);
                }
                if (location.X < player.location.X && location.Z < player.location.Z)
                {
                    rotationAngle = (float)Math.Atan((double)((location.X - player.location.X) / (location.Z - player.location.Z)));
                }
                if (location.X < player.location.X && location.Z > player.location.Z)
                {
                    rotationAngle = (float)Math.Atan((double)((location.X - player.location.X) / (location.Z - player.location.Z)))
                                - MathHelper.ToRadians(180);
                }
                if (location.X > player.location.X && location.Z < player.location.Z)
                {
                    rotationAngle = (float)Math.Atan((double)((location.X - player.location.X) / (location.Z - player.location.Z)))
                                - MathHelper.ToRadians(0);
                }


                if (!poppingUp && !poppingDown)
                {
                    if (popUp == 1)
                    {
                        poppingUp = true;
                    }   
                }
            }
            
            if (poppingUp)
            {
                if (location.Y < 2)
                    location.Y += .5f;
                if (location.Y >= 2)
                {
                    poppingUp = false;
                    poppingDown = true;
                    sound.PlayCue("throw");
                    throwFood(foodArray, foodRandom, player);
                }
            }

            if (poppingDown)
            {
                if (location.Y > -5)
                    location.Y -= .5f;
                if (location.Y <= -5)
                {
                    poppingDown = false;
                }
            }

            if (moving)
            {
                if (move(pitArray))
                {
                    moving = false;
                }
                //change holenum when done //should be ok
                //set moving to false when done
            }
        }

        public bool move(Holes[] pitArray)
        {
            location.Y = 0;
            velocity = Vector3.Normalize(new Vector3(pitArray[holeNum].position.X - location.X, 0, pitArray[holeNum].position.Z - location.Z));
            location += new Vector3(velocity.X, 0, velocity.Z);

            if ((location.X > (pitArray[holeNum].position.X + .5f) || location.X > (pitArray[holeNum].position.X - .5f)) && 
                (location.Z > (pitArray[holeNum].position.Z + .5f) || location.Z > (pitArray[holeNum].position.Z - .5f)))
            {
                location = new Vector3 (pitArray[holeNum].position.X, 2f, pitArray[holeNum].position.Z);
                poppingDown = true;
                return true;
            }
            else
                return false;
        }

        public void throwFood(Foods[] foodArray, int foodRandom, Kathy player)
        {
            for (int i = 0 ; i < 60 ; i++)
            {
                if (!foodArray[foodRandom].thrown)
                {
                    foodArray[foodRandom].thrown = true;
                    foodArray[foodRandom].position = location;
                    foodArray[foodRandom].position.Y += 7;
                    Vector3 test = new Vector3(location.X - player.location.X, 0, location.Z - player.location.Z);
                    foodArray[foodRandom].velocity = Vector3.Normalize(test) * -((float)foodArray[foodRandom].Strength / 10f);
                    i = 120;
                    return;
                }
                else
                    foodRandom = (foodRandom + 1) % 60;
            }
            for (int i = 0; i < 60; i++)
            {
                foodArray[i].thrown = false;
                foodArray[i].velocity = new Vector3(0, 0, 0);
                foodArray[i].position = new Vector3(-60, 100, 0);
            }

        }

        public void draw(Matrix world, Matrix view, Matrix projection)
        {
            for (int index = 0; index < ChefModel.Meshes.Count; index++)
            {
                ModelMesh mesh = ChefModel.Meshes[index];
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.PreferPerPixelLighting = true;
                    effect.EnableDefaultLighting();
                    effect.World = mesh.ParentBone.Transform * world;
                    effect.View = view;
                    effect.Projection = projection;
                }

                mesh.Draw();
            }

        }

    }
}
